package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.dto.UserRegisterRequest;
import com.tencent.wxcloudrun.dto.UserLoginRequest;
import com.tencent.wxcloudrun.dto.UserInfoResponse;
import com.tencent.wxcloudrun.model.UserInfo;
import com.tencent.wxcloudrun.model.ActivityInfo;
import com.tencent.wxcloudrun.model.Comment;
import com.tencent.wxcloudrun.service.UserService;
import com.tencent.wxcloudrun.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;

@RestController
@RequestMapping("/login")
public class LoginController {

    private final UserService userService;

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    public ApiResponse register(@RequestBody UserRegisterRequest request) {
        // 检查是否已注册
        if (userService.getUserByWeId(request.getWeId()) != null) {
            return ApiResponse.error(201, "用户已存在");
        }

        // 创建用户对象
        UserInfo newUser = new UserInfo();
        newUser.setWeId(request.getWeId());
        newUser.setUserName(request.getUserName());
        newUser.setPhone(request.getPhone());

        // 执行注册
        userService.registerUser(newUser);
        
        return ApiResponse.ok("注册成功");
    }

    @PostMapping("/login")
    public ApiResponse login(@RequestBody UserLoginRequest request) {
        // 根据weId获取用户信息
        UserInfo user = userService.getUserByWeId(request.getWeId());
        
        if (user == null) {
            return ApiResponse.error(202, "用户未注册");
        }

        // 构造响应数据
        UserInfoResponse response = new UserInfoResponse();
        response.setUserInfo(user);
        response.setActivityInfo(Collections.emptyList()); // 示例数据
        response.setPersonComment(Collections.emptyList());
        response.setInstitutionComment(Collections.emptyList());

        return ApiResponse.ok("登录成功", response);
    }
} 